package com.example.socialwebserver.service.impl;

import com.example.socialwebserver.pojo.*;
import com.example.socialwebserver.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class UserMatchServiceImpl implements UserMatch {
    @Autowired
    UserService userService;
    @Autowired
    FriendService friendService;
    @Autowired
    ExpectFriendService expectFriendService;
    @Autowired
    ExpectLoveService expectLoveService;

    @Override
    public List<MatchDAO> MatchFriend(Integer id) {
        ExpectFriendDAO expectFriendDAO = expectFriendService.SelectById(id);
        List<UserDAO> userDAOList = userService.SelectAll();
        List<MatchDAO> list = new ArrayList<>();
        for (UserDAO userDAO : userDAOList) {
            if(userDAO.getId() != id){
                int cnt = 0;
                String str[], str1[];
                str = userDAO.getHobby().split(" ");//获取用户的爱好
                str1 = expectFriendDAO.getHobby().split(" ");//自己的爱好
                for (int i = 0; i < str.length; i++) {
                    for (int j = 0; j < str1.length; j++) {
                        if (str[i].equals(str1[j])) {
                            cnt++;
                        }
                    }
                }
                str = userDAO.getCharac().split(" ");
                str1 = expectFriendDAO.getCharac().split(" ");
                for (int i = 0; i < str.length; i++) {
                    for (int j = 0; j < str1.length; j++) {
                        System.out.println(str[i] + " " + str1[j]);
                        if (str[i].equals(str1[j])) {
                            cnt++;
                        }
                    }
                }
                float f = cnt * (float) (100 / 6);
                System.out.println(f);
                list.add(new MatchDAO(f, userDAO.getId()));
            }

        }
        Collections.sort(list);
        return list;
    }

    @Override
    public List<MatchDAO> MatchLove(Integer id) {
        ExpectLoveDAO expectLoveDAO = expectLoveService.SelectById(id);
        List<UserDAO> userDAOList = userService.SelectAll();
        List<MatchDAO> list = new ArrayList<>();
        for (UserDAO userDAO : userDAOList) {
            if(userDAO.getId() != id && userDAO.getSex().equals(expectLoveDAO.getSex())){
                int cnt = 0;
                String str[], str1[];
                str = userDAO.getHobby().split(" ");//获取用户的爱好
                str1 = expectLoveDAO.getHobby().split(" ");//自己的爱好
                for (int i = 0; i < str.length; i++) {
                    for (int j = 0; j < str1.length; j++) {
                        if (str[i].equals(str1[j])) {
                            cnt++;
                        }
                    }
                }
                str = userDAO.getCharac().split(" ");
                str1 = expectLoveDAO.getCharac().split(" ");
                for (int i = 0; i < str.length; i++) {
                    for (int j = 0; j < str1.length; j++) {
                        if (str[i].equals(str1[j])) {
                            cnt++;
                        }
                    }
                }
                float f = cnt * (float) (50 / 6);
                float height = userDAO.getHeight();
                float height1 = expectLoveDAO.getHeight();
                if (height1 <= height) {
                    f += 25;
                } else if (height1 - height <= 5) {
                    f += 15;
                }
                Integer ap = userDAO.getAppearance();
                Integer ap1 = expectLoveDAO.getApperance();
                if (ap1 <= ap) {
                    f += 25;
                } else if (ap1 - ap <= 5) {
                    f += 15;
                }
                list.add(new MatchDAO(f, userDAO.getId()));
            }
        }
        Collections.sort(list);
        return list;
    }
}
